package com.syni.mdd.yhd.common.all.service;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsDxClientRoleDao;
import com.syni.mdd.yhd.common.all.entity.BmsDxClientRole;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.sjb.common.component.utils.redis.utils.RedisTemplateUtils;;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @className BmsUserOperationLogService
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsDxClientRoleService extends BaseService<BmsDxClientRole, Integer> {

    //普通用户
    final static int generalUserRoleId = 1;
    //商家管理员
    final static int bussinessRoleId = 2;
    //店员
    final static int assistantRoleId = 3;


    @Autowired
    BmsDxClientRoleDao bmsDxClientRoleDao;

    @Override
    public BmsDxClientRoleDao getBaseDao() {
        return bmsDxClientRoleDao;
    }

    public Map<String,Object> addRegisterUserRole(Integer userid,Integer roleId){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("status", ResultCode.CODE_ERROR);
        if(roleId == null){
            return resultMap;
        }
        BmsDxClientRole bmsDxClientRole;
        List<BmsDxClientRole> bmsDxClientRoles = bmsDxClientRoleDao.findByBmsUserIdAndRoleId(userid,roleId);
        if(bmsDxClientRoles != null && bmsDxClientRoles.size() > 0) {
            bmsDxClientRole = bmsDxClientRoles.get(0);
        }else {
            bmsDxClientRole = new BmsDxClientRole();
            bmsDxClientRole.setBmsUserId(userid);
            bmsDxClientRole.setRoleId(roleId);
            bmsDxClientRole.setNewTime(new Date());
            bmsDxClientRoleDao.save(bmsDxClientRole);
        }
        resultMap.put("status",ResultCode.CODE_NORMAL);
        resultMap.put("data",bmsDxClientRole);
        return resultMap;
    }

    public void addRegisterUserRole(Integer userid,Integer roleId,Date nowTime){
        BmsDxClientRole bmsDxClientRole = new BmsDxClientRole();
        bmsDxClientRole.setBmsUserId(userid);
        bmsDxClientRole.setRoleId(roleId);
        bmsDxClientRole.setNewTime(nowTime);
        bmsDxClientRoleDao.save(bmsDxClientRole);
    }

    public List<BmsDxClientRole> getUserClienRole(Integer userId){
        return bmsDxClientRoleDao.findByBmsUserId(userId);
    }

    public Map<Integer, BmsDxClientRole> getUserClienRole(Set userIds,Integer roleId){
        Map<Integer, BmsDxClientRole> bmsUserMap = new HashMap<>();
        Set<Integer> roleIds = new HashSet<>();
        roleIds.add(roleId);
        List<BmsDxClientRole> bmsDxClientRoles = bmsDxClientRoleDao.findByBmsUserIdInAndRoleIdIn(userIds,roleIds);
        if(bmsUserMap != null && bmsDxClientRoles.size() > 0){
            for(BmsDxClientRole bmsDxClientRole : bmsDxClientRoles){
                bmsUserMap.put(bmsDxClientRole.getBmsUserId(),bmsDxClientRole);
            }
        }
        return bmsUserMap;
    }

    public List<BmsDxClientRole> getUserClienRole(Integer userId,Integer roleId){
        Set<Integer> roleIds = new HashSet<>();
        roleIds.add(roleId);
        return bmsDxClientRoleDao.findByBmsUserIdAndRoleIdIn(userId,roleIds);
    }

    public BmsDxClientRole getUserRoleCache(Integer userId,Integer roleId){
        /*String redisKey = Constants.REDIS_KEY_USER_ROLE + String.format("%s:%s",userId,roleId);
        if(RedisTemplateUtils.getRedisTemplate().hasKey(redisKey)){
            BmsDxClientRole bmsDxClientRole = (BmsDxClientRole) RedisTemplateUtils.getRedisTemplate().opsForValue().get(redisKey);
            return bmsDxClientRole;
        }*/
        List<BmsDxClientRole> bmsDxClientRoles = bmsDxClientRoleDao.findByBmsUserIdAndRoleId(userId,roleId);
        if(bmsDxClientRoles != null && bmsDxClientRoles.size() > 0){
            BmsDxClientRole bmsDxClientRole = bmsDxClientRoles.get(0);
            //缓存起来
            //RedisTemplateUtils.getRedisTemplate().opsForValue().set(redisKey,bmsDxClientRole ,1,TimeUnit.DAYS);
            return bmsDxClientRoles.get(0);
        }
        return null;
    }

    public BmsDxClientRole getUserClientRole(Integer userId,Integer roleId){
        BmsDxClientRole bmsDxClientRole = null;
        List<BmsDxClientRole> bmsDxClientRoles = bmsDxClientRoleDao.findByBmsUserIdAndRoleId(userId,roleId);
        if(bmsDxClientRoles != null && bmsDxClientRoles.size() > 0){
            bmsDxClientRole = bmsDxClientRoles.get(0);
        }else {
            bmsDxClientRole = new BmsDxClientRole();
            bmsDxClientRole.setRoleId(roleId);
            bmsDxClientRole.setBmsUserId(userId);
            bmsDxClientRole.setNewTime(new Date());
            bmsDxClientRoleDao.save(bmsDxClientRole);
        }
        return bmsDxClientRole;
    }

    public List<BmsDxClientRole> getUserClienRole(Integer userId,Set roleIds){
        return bmsDxClientRoleDao.findByBmsUserIdAndRoleIdIn(userId,roleIds);
    }

    public boolean checkUserHavingBusinessOrAssistant(Integer userId){
        boolean ret = false;
        List<BmsDxClientRole> bmsDxClientRoleList = this.getUserClienRole(userId);
        if(bmsDxClientRoleList != null && bmsDxClientRoleList.size() > 0){
            //查看用户是否有商户或店员的角色
            for(BmsDxClientRole bmsDxClientRole : bmsDxClientRoleList){
                if(bmsDxClientRole.getRoleId().intValue() == BmsDxClientRoleService.bussinessRoleId
                        || bmsDxClientRole.getRoleId().intValue() == BmsDxClientRoleService.assistantRoleId){
                    ret = true;
                }
            }
        }
        return ret;
    }

    public boolean checkUserHavingBusiness(List<BmsDxClientRole> bmsDxClientRoleList){
        boolean ret = false;
        if(bmsDxClientRoleList != null && bmsDxClientRoleList.size() > 0){
            //查看用户是否有商户或店员的角色
            for(BmsDxClientRole bmsDxClientRole : bmsDxClientRoleList){
                if(bmsDxClientRole.getRoleId().intValue() == BmsDxClientRoleService.bussinessRoleId){
                    ret = true;
                }
            }
        }
        return ret;
    }

    public boolean checkUserHavingAssistant(List<BmsDxClientRole> bmsDxClientRoleList){
        boolean ret = false;
        if(bmsDxClientRoleList != null && bmsDxClientRoleList.size() > 0){
            //查看用户是否有商户或店员的角色
            for(BmsDxClientRole bmsDxClientRole : bmsDxClientRoleList){
                if(bmsDxClientRole.getRoleId().intValue() == BmsDxClientRoleService.assistantRoleId){
                    ret = true;
                }
            }
        }
        return ret;
    }

    public void flushRole(BmsDxClientRole bmsDxClientRole) {
        bmsDxClientRoleDao.save(bmsDxClientRole);
        //String redisKey = Constants.REDIS_KEY_USER_ROLE + String.format("%s:%s",bmsDxClientRole.getBmsUserId(),bmsDxClientRole.getRoleId());
        //RedisTemplateUtils.getRedisTemplate().opsForValue().set(redisKey,bmsDxClientRole ,1,TimeUnit.DAYS);
    }
}
